package com.jiepos.mpos.biz.appInfM.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jiepos.mpos.common.constant.DBConstant;
import com.jiepos.mpos.common.constant.SystemConstant;
import com.jiepos.mpos.common.dao.AppClientFeedBackDao;
import com.jiepos.mpos.common.entity.App_Client_FeedBackEntity;
import com.jiepos.mpos.common.entity.App_Ins_InfEntity;
import com.jiepos.mpos.core.entity.Pagination;
import com.jiepos.mpos.core.service.BaseService;
import com.jiepos.mpos.core.util.IDFactoryUtil;
import com.jiepos.mpos.core.util.StringUtils;


/**
 *
 *
 * <p>标题: —— 客户端反馈信息表类</p>
 * <p>模块: 通用平台</p>
 * <p>版权: Copyright (c) 2017 江苏杰博实信息技术有限公司
 * @ProjectName:  [mpos]
 * @Package com.jiepos.mpos.biz.appInfM.service
 * @author 余翼膑
 * @version 1.0
 * @since 2017年9月28号
 * @history（历次修订内容、修订人、修订时间等）
 */

@Service
public class FeedbackService extends BaseService {
    @Autowired
    private AppClientFeedBackDao appClientFeedBackDao;

    /**
     * 查询通道信息列表json分页数据
     * @param pagination
     * @param appClientFeedBackEntity
     * @return
     */
    public Map getFeedBackList(Pagination pagination, App_Client_FeedBackEntity appClientFeedBackEntity,App_Ins_InfEntity appInsInfEntity) {
        StringBuffer sqlBuf = getInitSql("t1").append(",");
        sqlBuf.append("  t1.feedback_uuid AS UUID,t1.title,t1.app_name,t2.custom_no||'-'||t2.name login_name,t1.app_type||'-'||t3.code_desc as app_type,  ")
                .append(" T4.INS_CD||'-'||T4.INS_CN_NM  AS INST_NAME ")
                .append(" FROM  ")
                .append(DBConstant.Table.T_APP_CLIENT_FEEDBACK+"  T1  ")
                .append(" LEFT JOIN ")
                .append(DBConstant.Table.T_APP_CUSTOM_INF)
                .append( " T2 ON T1.CUSTOM_UUID =T2.CUSTOM_UUID ")
                .append(" LEFT JOIN ")
                .append(DBConstant.Table.T_APP_CODE_LIST).append(" t3 ON t1.APP_TYPE = t3.code_val and t3.code_tp='APP_TYPE' ")
                .append(" LEFT JOIN  "+ DBConstant.Table.T_APP_INS_INF +" t4 on t1.ins_uuid = t4.ins_uuid  ")
                .append(" where 1 = 1 ");
        List list = new ArrayList();
       if (!StringUtils.isBlank(appClientFeedBackEntity.getApp_name())) {
           sqlBuf.append( " and t1.app_name like ? " );
            list.add("%" + appClientFeedBackEntity.getApp_name() + "%");
        }
        if (!StringUtils.isBlank(appClientFeedBackEntity.getApp_type())) {
            sqlBuf.append( " and t1.app_type like ? " );
            list.add("%" + appClientFeedBackEntity.getApp_type() + "%");
        }
        //机构号
        if (!StringUtils.isBlank(appInsInfEntity.getIns_cd())) {
            sqlBuf.append(" and t4.ins_cd like ? ");
            list.add("%" + appInsInfEntity.getIns_cd() + "%");
        }
        //机构名称
        if (!StringUtils.isBlank(appInsInfEntity.getIns_cn_nm())) {
            sqlBuf.append(" and t4.ins_cn_nm like ? ");
            list.add("%" + appInsInfEntity.getIns_cn_nm() + "%");
        }
        pagination.queryString = sqlBuf.toString();
        pagination = this.getOrderSql(pagination, "t1");
        return appClientFeedBackDao.getPaginationMap(pagination, "t1", list);
    }


    /**
     * 根据记录id查询实体对象
     * @param
     * @param feedback_uuid
     * @return
     */
    public Map getFeedBackById(String feedback_uuid) {
        if (StringUtils.isBlank(feedback_uuid) || feedback_uuid.equals("undefined")) {
            return new HashMap();
        }
        StringBuffer sqlBuf = getInitSql("t1").append(",");
        sqlBuf.append(" t1.feedback_uuid,t1.title,T1.app_type,t1.system_device_model,t1.app_name,t1.system_uuid,t1.system_name,t1.system_version,t1.content,t1.rec_id   ")
                .append(" FROM  ")
                .append(" T_APP_CLIENT_FEEDBACK T1  ")
                .append(" where t1.feedback_uuid=?");
        List<String> list = new ArrayList<>();
        list.add(feedback_uuid);
        List<Map<String, Object>> results = appClientFeedBackDao.queryForList(getOrderSql(sqlBuf.toString(), "t1"), list);
        return results != null ? results.get(0) : new HashMap<String, Object>();
    }

    public void saveFeedBack(App_Client_FeedBackEntity appClientFeedBackEntity, Map map) {
        //当前用户
        appClientFeedBackEntity.setRec_upd_usr_id(String.valueOf(map.get(SystemConstant.SESSION_LOGIN_USER)));
       /* if(org.apache.commons.lang.StringUtils.isBlank(appInsAppEntity.getIns_uuid())){
            appInsAppEntity.setIns_uuid(String.valueOf(map.get(SystemConstant.SESSION_LOGIN_INS_UUID)));
        }*/
        if (!StringUtils.isBlank(appClientFeedBackEntity.getFeedback_uuid())) {
            appClientFeedBackEntity = (App_Client_FeedBackEntity) this.saveCommonData(appClientFeedBackEntity ,map,"update" );
            List noChangeList = new ArrayList();
            noChangeList.add("feedback_uuid");
            noChangeList.add("rec_crt_ts");
            appClientFeedBackDao.update(appClientFeedBackEntity, noChangeList);
        } else {
            //新增
            appClientFeedBackEntity.setLast_oper_in(SystemConstant.OPER_ADD);
            appClientFeedBackEntity.setFeedback_uuid(IDFactoryUtil.getUUID());
            App_Client_FeedBackEntity newappClientFeedBackEntity = (App_Client_FeedBackEntity) this.saveCommonData(appClientFeedBackEntity, map, "null");
            appClientFeedBackDao.save(appClientFeedBackEntity);
        }
    }
}
